package threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-20 11:28
 **/
public class T08_SingleThreadPool {


    public static void main(String[] args) {
        /**
         *
         *  =======> Executors.newSingleThreadExecutor();
         *  ---------------------------------------------------------------------------------
         *  return new FinalizableDelegatedExecutorService
         *                             //最大和最小线程数都是 1 ,永远只有一个线程存活
         *      (new ThreadPoolExecutor(1, 1,
         *                             // 等待时间是 0 ，没用就灭活
         *                              0L, TimeUnit.MILLISECONDS,
         *                               **
         *                                public LinkedBlockingQueue() {
         *                                 this(Integer.MAX_VALUE);
         *                                }
         *                               ****
         *                               // 这个队列中线程的最大值是 Integer.MAX_VALUE
         *                              new LinkedBlockingQueue<Runnable>()));
         */
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        for (int i = 0; i < 5; i++) {
            final int j = i;
            executorService.submit(() -> {
                System.err.println(Thread.currentThread().getName() + "=====> " + j);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();
    }
}
